package com.grass.p019lv.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;
import androidx.core.app.NotificationCompat;
import com.androidsa.bttsddka.R;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.event.VoiceProgressEvent;
import com.androidx.p013lv.base.bean.novel.NovelChapterBean;
import com.androidx.p013lv.base.event.VoicePlayLoadingEvent;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.http.HttpUtils;
import com.androidx.p013lv.base.http.UrlManager;
import com.androidx.p013lv.base.http.callback.HttpCallback;
import com.androidx.p013lv.base.utils.LogUtils;
import com.grass.p019lv.App;
import com.grass.p019lv.activity.MainActivity;
import com.grass.p019lv.event.EventBusEvent;
import com.grass.p019lv.event.EventBusUtils;
import com.grass.p019lv.event.NovelCommentNumEvent;
import com.grass.p019lv.event.VoiceErrorEvent;
import com.grass.p019lv.event.VoicePlayStatusEvent;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import p023io.reactivex.Observable;
import p023io.reactivex.android.schedulers.AndroidSchedulers;
import p023io.reactivex.disposables.Disposable;
import p023io.reactivex.functions.Consumer;

/* renamed from: com.grass.lv.service.MediaService */
public class MediaService extends Service {
    private static final String CHANNEL_ID = "channel_002";
    private static final String GROUP_ID = "group_002";
    public static final String NOTIFY_CLOSE = "pure_music.kunminx.close";
    public static final String NOTIFY_NEXT = "pure_music.kunminx.next";
    public static final String NOTIFY_PAUSE = "pure_music.kunminx.pause";
    public static final String NOTIFY_PLAY = "pure_music.kunminx.play";
    public static final String NOTIFY_PREVIOUS = "pure_music.kunminx.previous";
    private static final String TAG = "MediaService";
    public static final int UPDATE_SHOW_PAUSE = 2;
    public static final int UPDATE_SHOW_PLAY = 1;
    public static MediaPlayer mMediaPlayer;
    private List<Integer> music;
    private Notification notification;
    private int novelId;
    private RemoteViews remoteView;
    private int seek;
    private RemoteViews simpleContentView;
    private Disposable subscribe;
    private WifiManager.WifiLock wifiLock;

    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onCreate() {
        EventBusUtils.register(this);
        mMediaPlayer = new MediaPlayer();
        super.onCreate();
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        if (intent == null) {
            return 2;
        }
        this.music = (List) intent.getSerializableExtra(Constance.service_data);
        this.novelId = intent.getIntExtra(Constance.service_novelId, 0);
        List<Integer> list = this.music;
        if (list == null || list.size() <= 0) {
            return 2;
        }
        createNotification();
        App.chapterId = this.music.get(0).intValue();
        App.novelId = this.novelId;
        getNovelChpaterInfo(App.novelId, App.chapterId);
        return 2;
    }

    public void getNovelChpaterInfo(int i, int i2) {
        HttpUtils.getInsatance().get(UrlManager.getInsatance().getNovelChapterInfo(i, i2), new HttpCallback<BaseRes<NovelChapterBean>>("novelChapterInfo") {
            /* class com.grass.p019lv.service.MediaService.C32251 */

            public void onLvSuccess(BaseRes<NovelChapterBean> baseRes) {
                if (200 == baseRes.getCode()) {
                    int commentNum = baseRes.getData().getCommentNum();
                    App.chapterId = baseRes.getData().getChapterId();
                    EventBus.getDefault().post(new NovelCommentNumEvent(commentNum, App.chapterId, baseRes.getData().getChapterTitle()));
                    if (!TextUtils.isEmpty(baseRes.getData().getPlayPath())) {
                        MediaService.this.iniMediaPlayerFile(App.getProxy().getProxyUrl(baseRes.getData().getPlayPath()));
                        return;
                    }
                    EventBus.getDefault().post(new VoiceErrorEvent(baseRes.getData().getReasonType()));
                }
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void iniMediaPlayerFile(String str) {
        try {
            EventBus.getDefault().post(new VoicePlayLoadingEvent(true));
            mMediaPlayer.setWakeMode(this, 1);
            WifiManager.WifiLock createWifiLock = ((WifiManager) getApplicationContext().getSystemService("wifi")).createWifiLock(1, "wifilock");
            this.wifiLock = createWifiLock;
            createWifiLock.acquire();
            mMediaPlayer.reset();
            mMediaPlayer.setLooping(false);
            mMediaPlayer.setDisplay(null);
            mMediaPlayer.setDataSource(str);
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                /* class com.grass.p019lv.service.MediaService.C32262 */

                public void onPrepared(MediaPlayer mediaPlayer) {
                    EventBus.getDefault().post(new VoicePlayLoadingEvent(false));
                    MediaService.this.playNoSeek();
                }
            });
            mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                /* class com.grass.p019lv.service.MediaService.C32273 */

                public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
                    return true;
                }
            });
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                /* class com.grass.p019lv.service.MediaService.C32284 */

                public void onCompletion(MediaPlayer mediaPlayer) {
                    if (MediaService.this.subscribe != null) {
                        MediaService.this.subscribe.dispose();
                        MediaService.this.subscribe = null;
                    }
                    MediaService.this.nextMusic();
                }
            });
        } catch (IOException e) {
            hideNotification();
            EventBus.getDefault().post(new VoicePlayLoadingEvent(false));
            EventBus.getDefault().post(new VoiceErrorEvent(4));
            e.printStackTrace();
        }
    }

    public void playOrPause() {
        MediaPlayer mediaPlayer = mMediaPlayer;
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
                postState(1, true);
                return;
            }
            mMediaPlayer.start();
            postState(2, false);
        }
    }

    public void playNoSeek() {
        MediaPlayer mediaPlayer = mMediaPlayer;
        if (mediaPlayer != null) {
            mediaPlayer.start();
            postState(2, false);
        }
    }

    private void updateNotification() {
        MediaPlayer mediaPlayer = mMediaPlayer;
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                EventBus.getDefault().post(new VoicePlayStatusEvent(true));
                this.simpleContentView.setViewVisibility(R.id.player_pause, 0);
                this.simpleContentView.setViewVisibility(R.id.player_play, 8);
            } else {
                EventBus.getDefault().post(new VoicePlayStatusEvent(false));
                this.simpleContentView.setViewVisibility(R.id.player_pause, 8);
                this.simpleContentView.setViewVisibility(R.id.player_play, 0);
            }
            this.notification.contentView = this.simpleContentView;
            showNotification();
        }
    }

    public void showNotification() {
        if (Build.VERSION.SDK_INT >= 26) {
            startForeground(5, this.notification);
        } else if (this.notification != null) {
            ((NotificationManager) getSystemService("notification")).notify(5, this.notification);
        }
    }

    public void hideNotification() {
        if (Build.VERSION.SDK_INT >= 26) {
            stopForeground(true);
        } else if (this.notification != null) {
            ((NotificationManager) getSystemService("notification")).cancel(5);
        }
    }

    public void play() {
        MediaPlayer mediaPlayer = mMediaPlayer;
        if (mediaPlayer != null) {
            mediaPlayer.start();
            mMediaPlayer.seekTo(this.seek);
            postState(2, false);
        }
    }

    public void pause() {
        MediaPlayer mediaPlayer = mMediaPlayer;
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
            postState(1, true);
            Log.i(TAG, "Play stop");
        }
    }

    private void postState(int i, boolean z) {
        updateNotification();
        if (i == 1) {
            Disposable disposable = this.subscribe;
            if (disposable != null) {
                disposable.dispose();
                this.subscribe = null;
            }
        } else if (i == 2) {
            sendMessage();
        }
    }

    private void sendMessage() {
        Disposable disposable = this.subscribe;
        if (disposable != null) {
            disposable.dispose();
            this.subscribe = null;
        }
        this.subscribe = Observable.interval(0, 1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
            /* class com.grass.p019lv.service.MediaService.C32295 */

            public void accept(Long l) throws Exception {
                if (MediaService.this.music != null && MediaService.mMediaPlayer != null) {
                    EventBus.getDefault().post(new VoiceProgressEvent(MediaService.mMediaPlayer.getDuration(), MediaService.mMediaPlayer.getCurrentPosition()));
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(EventBusEvent eventBusEvent) {
        int code = eventBusEvent.getCode();
        if (code == 1118503) {
            LogUtils.m63e("MMMMMMMMMMMMMMMMMM", "=======SERVICE_NEXT");
            nextMusic();
        } else if (code != 1118528) {
            switch (code) {
                case EventBusUtils.EventCode.SERVICE_NOTIFICATION_CLOSE:
                    LogUtils.m63e("MMMMMMMMMMMMMMMMMM", "=======SERVICE_NOTIFICATION_CLOSE");
                    return;
                case EventBusUtils.EventCode.SERVICE_PLAY:
                    LogUtils.m63e("MMMMMMMMMMMMMMMMMM", "=======SERVICE_PLAY");
                    playNoSeek();
                    return;
                case EventBusUtils.EventCode.SERVICE_PAUSE:
                    LogUtils.m63e("MMMMMMMMMMMMMMMMMM", "=======SERVICE_PAUSE");
                    pause();
                    return;
                case EventBusUtils.EventCode.SERVICE_SEEK:
                    LogUtils.m63e("MMMMMMMMMMMMMMMMMM", "=======SERVICE_SEEK");
                    pause();
                    this.seek = ((Integer) eventBusEvent.getData()).intValue();
                    play();
                    return;
                default:
                    switch (code) {
                        case EventBusUtils.EventCode.SERVICE_NOTIFICATION_OPEN:
                            LogUtils.m63e("MMMMMMMMMMMMMMMMMM", "=======SERVICE_NOTIFICATION_OPEN");
                            return;
                        case EventBusUtils.EventCode.SERVICE_NEXT_PREPARE:
                            LogUtils.m63e("MMMMMMMMMMMMMMMMMM", "=======SERVICE_NEXT_PREPARE");
                            prepareNextMusic();
                            return;
                        case EventBusUtils.EventCode.SERVICE_STOP:
                            MediaPlayer mediaPlayer = mMediaPlayer;
                            if (mediaPlayer != null) {
                                mediaPlayer.reset();
                                mMediaPlayer.stop();
                            }
                            Disposable disposable = this.subscribe;
                            if (disposable != null) {
                                disposable.dispose();
                                this.subscribe = null;
                            }
                            EventBus.getDefault().post(new VoicePlayStatusEvent(false));
                            App.novelId = 0;
                            hideNotification();
                            return;
                        case EventBusUtils.EventCode.SERVICE_SELECT:
                            App.chapterId = ((Integer) eventBusEvent.getData()).intValue();
                            getNovelChpaterInfo(App.novelId, App.chapterId);
                            return;
                        default:
                            return;
                    }
            }
        } else {
            playOrPause();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void nextMusic() {
        List<Integer> list = this.music;
        if (list != null && list.size() > 0) {
            int indexOf = this.music.indexOf(Integer.valueOf(App.chapterId));
            if (indexOf != this.music.size() - 1) {
                App.chapterId = this.music.get(indexOf + 1).intValue();
                getNovelChpaterInfo(App.novelId, App.chapterId);
                return;
            }
            EventBus.getDefault().post(new VoiceErrorEvent(5));
        }
    }

    private void prepareNextMusic() {
        List<Integer> list = this.music;
        if (list != null && list.size() > 0) {
            int indexOf = this.music.indexOf(Integer.valueOf(App.chapterId));
            if (indexOf != 0) {
                App.chapterId = this.music.get(indexOf - 1).intValue();
                getNovelChpaterInfo(this.novelId, App.chapterId);
                return;
            }
            EventBus.getDefault().post(new VoiceErrorEvent(5));
        }
    }

    private void createNotification() {
        try {
            this.simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), (int) R.layout.notify_player_small);
            int i = 0;
            PendingIntent activity = PendingIntent.getActivity(this, 0, new Intent(getApplicationContext(), MainActivity.class), 0);
            if (Build.VERSION.SDK_INT >= 26) {
                NotificationManager notificationManager = (NotificationManager) getSystemService("notification");
                notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(GROUP_ID, getString(R.string.play)));
                NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, getString(R.string.notify_of_play), 2);
                notificationChannel.setGroup(GROUP_ID);
                notificationManager.createNotificationChannel(notificationChannel);
            }
            Notification build = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID).setSmallIcon(R.drawable.ic_player).setContentIntent(activity).setOnlyAlertOnce(true).setContentTitle("").build();
            this.notification = build;
            build.contentView = this.simpleContentView;
            setListeners(this.simpleContentView);
            this.notification.contentView.setViewVisibility(R.id.player_progress_bar, 8);
            this.notification.contentView.setViewVisibility(R.id.player_next, 0);
            this.notification.contentView.setViewVisibility(R.id.player_previous, 0);
            boolean isPlaying = mMediaPlayer.isPlaying();
            this.notification.contentView.setViewVisibility(R.id.player_pause, isPlaying ? 8 : 0);
            RemoteViews remoteViews = this.notification.contentView;
            if (!isPlaying) {
                i = 8;
            }
            remoteViews.setViewVisibility(R.id.player_play, i);
            this.notification.contentView.setTextViewText(R.id.player_song_name, "");
            this.notification.contentView.setTextViewText(R.id.player_author_name, "");
            this.notification.flags |= 2;
            this.notification.contentView.setImageViewResource(R.id.player_album_art, R.drawable.ic_logo);
            startForeground(5, this.notification);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setListeners(RemoteViews remoteViews) {
        try {
            remoteViews.setOnClickPendingIntent(R.id.player_previous, PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("pure_music.kunminx.previous").setPackage(getPackageName()), 134217728));
            remoteViews.setOnClickPendingIntent(R.id.player_close, PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("pure_music.kunminx.close").setPackage(getPackageName()), 134217728));
            remoteViews.setOnClickPendingIntent(R.id.player_pause, PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("pure_music.kunminx.pause").setPackage(getPackageName()), 134217728));
            remoteViews.setOnClickPendingIntent(R.id.player_next, PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("pure_music.kunminx.next").setPackage(getPackageName()), 134217728));
            remoteViews.setOnClickPendingIntent(R.id.player_play, PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("pure_music.kunminx.play").setPackage(getPackageName()), 134217728));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onDestroy() {
        super.onDestroy();
        EventBusUtils.unregister(this);
        Disposable disposable = this.subscribe;
        if (disposable != null) {
            disposable.dispose();
            this.subscribe = null;
        }
        MediaPlayer mediaPlayer = mMediaPlayer;
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
        WifiManager.WifiLock wifiLock2 = this.wifiLock;
        if (wifiLock2 != null) {
            wifiLock2.release();
        }
    }
}
